home *** CD-ROM | disk | FTP | other *** search
/ Cream of the Crop 25 / Cream of the Crop 25.iso / comm / qfaxv108.zip / MANYFAX.C < prev    next >
C/C++ Source or Header  |  1997-03-10  |  14KB  |  310 lines

  1. /*************************************************************************/
  2. /*  MANYFAX.C     Send many faxes automatically by calling QFAX.EXE      */
  3. /*          Copyright (C) 1997 Anthony Mai. All rights reserved          */
  4. /*                                                                       */
  5. /*  This C program serves two purposes:                                  */
  6. /*      1.To serve as an example how you can use spawnv() function to    */
  7. /*        call QFAX and send fax from your own C/C++ application         */
  8. /*      2.To serve as a useful program for fax broadcasting              */
  9. /*                                                                       */
  10. /*  You may modify and use this program for any purpose as long as this  */
  11. /*  copyright notice remains intact. But you may not re-distribute this  */
  12. /*  file except when you distribute it in it's original form, as part of */
  13. /*  the QFAXV108.ZIP shareware package. If you made any significant      */
  14. /*  modification to make it better, The original author would appreciate */
  15. /*  you send a copy to E-Mail QFAX@QFAX.COM if possible.                 */
  16. /*************************************************************************/
  17.  
  18. /*************************************************************************/
  19. /*  Usage of MANYFAX.EXE is like this:                                   */
  20. /*                                                                       */
  21. /*      MANYFAX [-f] [-db list_file]  [-log log_file]  file1 [file2] ... */
  22. /*                                                                       */
  23. /*  Items in [] means it is optional. The optional -f flag means it'll   */
  24. /*  fax *.FAX files directly, otherwise it assumes the files are text    */
  25. /*  files. The default list file is manyfax.lst. The default log file    */
  26. /*  is MANYFAX.LOG                                                       */
  27. /*************************************************************************/
  28.  
  29. #include <stdio.h>
  30. #include <conio.h>
  31. #include <process.h>
  32. #include <string.h>
  33. #include <time.h>
  34.  
  35. #define  TXT    0               /* Fax text files ************************/
  36. #define  FAX    1               /* Fax FAX files  ************************/
  37.  
  38. int filetype;                   /* Type fo files to be faxed: TXT or FAX */
  39. int successful, failure, total; /* Counting number of faxes sent/not sent*/
  40. int i,j,k,Ans, returncode;      /* A few public variables ****************/
  41. char qfaxcmd[128];              /* The QFAX.EXE command with it's path ***/
  42. char args[32][64];              /* Array of argument strings. Each string*/
  43.                                 /* less than 64 chars. Max 32 arguments **/
  44. char *arglist[33];              /* Array of pointers to arguments. Last **/
  45.                                 /* pointer must be NULL to terminate *****/
  46. char strname[16][16];           /* Name of used defined strings. Up to 16*/
  47.                                 /* names each shorter than 32 chars ******/
  48. char strings[16][64];           /* Up to 16 user defined strings, each is*/
  49.                                 /* Shorter than 64 chars *****************/
  50. char tmpstr[256];               /* A temprary string for file reading ****/
  51. char str1[64], str2[64];        /* Two temprary strings ******************/
  52. char faxnumber[48];             /* string containing the fax number ******/
  53.  
  54. char listName[64] = "manyfax.lst";   /* Name of default fax list file ****/
  55. char logName[64]  = "manyfax.log";   /* Name of default fax log file *****/
  56.  
  57. FILE * fFaxList, * fFaxLog, * fcfg; /* Fax list file, log file, cfg file */
  58.  
  59. void usage()
  60. {
  61.  printf("Usage of this program is:\n\n"
  62.         "  manyfax  [-db list_file] [-log log_file] [-f] file1 [file2] ...\n\n"
  63.         "Example:\n"
  64.         "  manyfax  -db myfax.lst  -log faxsend.log  cover.txt mylett.txt\n"
  65.         "This example sends file COVER.TXT and MYLETT.TXT to faxes listed\n"
  66.         "in file MYFAX.LST and write the fax result to file FAXSEND.LOG\n"
  67.         "Use '-f' option flag if you are faxing *.FAX files instead.\n"
  68.         "Please read USAGE.TXT for detailed usages.\n"
  69.         "E-Mail QFAX@QFAX.COM if there's a problem.\n");
  70. }
  71.  
  72. int readline()
  73. /* Process the info read from the fax list file. and decide what to do ***/
  74. /* returns: 0 -> The line is ignored. 1 -> It has just defined a string **/
  75. /* return 2 -> It has just read a fax number so it will fax out **********/
  76. {
  77.  int ii, jj, kk;
  78.  char onechar;
  79.  
  80.   str1[0] = '\0'; str2[0] = '\0';
  81.   sscanf(tmpstr, "%s", str1);           /* Read in the first word ********/
  82.  
  83.   /* This first word must end with a ':' to be useful. Or be discarded ***/
  84.   ii = jj = strlen(str1);
  85.   if (ii < 2) return(0);                /* It's a blank line, discard ****/
  86.  
  87.   if (str1[ii-1] != ':') return(0);     /* End of first word is not ':'***/
  88.  
  89.   ii--; str1[ii] = '\0';                /* Get rid of the ':' ************/
  90.   while (ii-- > 0) str1[ii] = toupper(str1[ii]); /* All upper case chars */
  91.  
  92.   if (strcmp(str1, "FAX")) { /* Is this first word "FAX"? none 0 -> No   */
  93.      /* The word is not "FAX" so it must be a string definition **********/
  94.      for (ii=0; ii<16; ii++) {          /* Overide same name string ******/
  95.         if (strcmp(str1, strname[ii])) {/* Not a matching name */}
  96.         else {                          /* Re-Define the matching string */
  97.           for (/*Nothing*/; jj < strlen(tmpstr); jj++) {
  98.              onechar = tmpstr[jj];      /* Skip the spaces and tabs ******/
  99.              if (onechar != ' ' && onechar != 0x09) break;
  100.             }
  101.           kk = 0; strings[ii][kk] = '\0';
  102.           for (/*Nothing*/; jj < strlen(tmpstr); jj++) {
  103.              onechar = tmpstr[jj];
  104.              if (onechar == ' ') onechar = '_'; /* Replace the space key */
  105.              if (onechar == 0x0d || onechar == 0x0a) break;
  106.              strings[ii][kk++] = onechar;
  107.             }
  108.           strings[ii][kk] = '\0';       /* Put a NULL to end of string ***/
  109.           return(1);
  110.          }
  111.        }
  112.  
  113.      for (ii=0; ii<15; ii++) {  /* No string of matching string name *****/
  114.         /* So find an empty string to define *****************************/
  115.         if (strname[ii][0] == '\0') break;
  116.        }
  117.      strcpy(strname[ii], str1);         /* Name of the string ************/
  118.  
  119.      for (/*Nothing*/; jj < strlen(tmpstr); jj++) {
  120.         onechar = tmpstr[jj];      /* Skip the spaces and tabs ******/
  121.         if (onechar != ' ' && onechar != 0x09) break;
  122.        }
  123.      kk = 0; strings[ii][kk] = '\0';
  124.      for (/*Nothing*/; jj < strlen(tmpstr); jj++) {
  125.         onechar = tmpstr[jj];
  126.         if (onechar == ' ') onechar = '_'; /* Replace the space key */
  127.         if (onechar == 0x0d || onechar == 0x0a) break;
  128.         strings[ii][kk++] = onechar;
  129.        }
  130.      strings[ii][kk] = '\0';       /* Put a NULL to end of string ***/
  131.      return(1);
  132.     }
  133.   else { /* Yes the word is "FAX", then get the fax number and return 2  */
  134.      ii = 0; faxnumber[ii] = '\0';
  135.      for (/*Nothing*/; jj<strlen(tmpstr); jj++) {
  136.        onechar = tmpstr[jj];
  137.        if (onechar > ' ' && onechar < ':') {
  138.            /* legitimate char for the fax number is between 0x21 and 0x39*/
  139.            faxnumber[ii++] = onechar;
  140.          }
  141.        if (ii>46) break;        /* The fax number is getting too long ****/
  142.       }
  143.      faxnumber[ii] = '\0';      /* Put a NULL at the end *****************/
  144.      return(2);                 /* Return value 2. Will fax immediately  */
  145.     }
  146. }
  147.  
  148.  
  149. void cleanstr()
  150. /* Clean up all string definitions */
  151. {
  152.   int ii;
  153.   for (ii=0; ii<16; ii++) {
  154.     strname[ii][0] = '\0'; strings[ii][0] = '\0';
  155.    }
  156. }
  157.  
  158. main(argc, argv)
  159.   int argc;
  160.   char * argv[];
  161. {
  162.  int fstnum, lstnum;    /* This identifies the first and last arguments */
  163.                         /* that are identified as name of files to fax  */
  164.  
  165.   _asm {
  166.     mov  ax, 03h
  167.     int  10h
  168.    } /*** This simply cleans the text mode screen *************************/
  169.  
  170.   if (argc == 1) {
  171.      usage(); exit(-1);
  172.     } /* If user typed "MANYFAX" only. Show a USAGE screen and exit. ******/
  173.  
  174.  /* Now construct the QFAX.EXE command, including the complete path ******/
  175.  /* First we assume QFAX.EXE and MANYFAX.EXE are under the same directory*/
  176.  /* argv[0] contains the complete path name of MANYFAX.EXE ***************/
  177.  
  178.  strcpy(qfaxcmd, argv[0]);
  179.  i = strlen(qfaxcmd) - 1;
  180.  while (i>0 && qfaxcmd[i] != '\\') i--;
  181.  qfaxcmd[i+1] = '\0';
  182.  
  183.  /* Now we have the complete path of MANYFAX.EXE, including the back slash*/
  184.  /* Just append "QFAX.EXE" to it and we have the QFAX command *************/
  185.  
  186.  strcat(qfaxcmd, "QFAX.EXE");
  187.  
  188.  /* Test whether the the QFAX path and file name is correct or not */
  189.  if ((fFaxList = fopen(qfaxcmd, "rb")) == NULL) {
  190.       printf("%s can not be found!\n", qfaxcmd); exit(-1);
  191.      }
  192.  else fclose(fFaxList);
  193.  
  194.  /* Now process the command line arguments *******************************/
  195.  /* First assuming we will fax TEXT files instead of FAX files ***********/
  196.  filetype = TXT;
  197.  fstnum = argc;
  198.  lstnum = 0;
  199.  i = 1;
  200.  while (i<argc) {
  201.     if (argv[i][0] == '-') switch (argv[i][1]) {
  202.       case 'd':
  203.       case 'D': i++; strcpy(listName, argv[i]); i++; break;
  204.       case 'l':
  205.       case 'L': i++; strcpy(logName,  argv[i]); i++; break;
  206.       case 'f':
  207.       case 'F': filetype = FAX; i++; break;
  208.      }
  209.     else { /* If it is not started by '-' it must be the file name *******/
  210.       fstnum = i; lstnum = argc - 1; break; /* Break out and continue ****/
  211.      }
  212.    }
  213.  
  214.  if (fstnum > lstnum) {
  215.     printf("You have not specified any file to be faxed on command line\n");
  216.     usage(); exit(-1);
  217.    }
  218.  
  219.   if ((fFaxList = fopen(listName, "rb")) == NULL) {
  220.      printf("Fax list file %s not found\n", listName); exit(-1);
  221.     }
  222.  
  223.   /* OK every thing is fine and the fax list and fax log file is opened */
  224.   /* We will read from the fax list and then call QFAX to fax out *******/
  225.  
  226.   printf("    Manyfax.exe is going to send faxes to all fax numbers listed\n"
  227.          "in the file  %s.  Results will be logged in  %s\n"
  228.          "The following files will be faxed:\n ", listName, logName);
  229.   for (i=fstnum; i<=lstnum; i++) printf(" %s", argv[i]);
  230.   printf("\n\nIt is ILLEGAL in USA and many other countries to send UNSOLICITED fax!\n");
  231.   printf("Are you sure you want to continue? Press 'c' to continue. 'q' to quit.\n");
  232.   while((Ans = getch()) != 'c' && Ans != 'q') {}
  233.   if (Ans != 'c') {
  234.      printf("Fax broadcasting aborted\n");
  235.      exit(-1);
  236.     }
  237.  
  238.   cleanstr(); /* Clean out all string definitions ***********************/
  239.   total = successful = failure;
  240.   while (!feof(fFaxList)) {
  241.     /* read a whole line into tmpstr ************************************/
  242.     tmpstr[0] = '\0'; /* Clean out the tmpstr first *********************/
  243.     fgets(tmpstr, 256, fFaxList);
  244.     j = readline();
  245.     switch(j) {
  246.        case 1:  break;                  /* A string has been defined ****/
  247.        case 2:  /* A fax number is read in. Will fax now ****************/
  248.                 /* So construct the argument list and fax out ***********/
  249.  
  250.                 j = 0; /* Start with the first argument to be "QFAX" ****/
  251.                 strcpy(args[j], "QFAX"); arglist[j] = args[j]; j++;
  252.                 strcpy(args[j], "-nod"); arglist[j] = args[j]; j++;
  253.                 if (filetype == FAX) {
  254.                    strcpy(args[j], "-f"); arglist[j] = args[j]; j++;
  255.                   }
  256.  
  257.                 if (fcfg = fopen("temp.cfg", "wb")) {
  258.                   /*** Write string definitions to temp.cfg ***************/
  259.                   for (i=0; i< 16; i++) { /* Add string definitions *******/
  260.                     if (strname[i][0] != '\0'  && strings[i][0] != '\0') {
  261.                       fprintf(fcfg, "Define %s %s\r\n",
  262.                         strname[i], strings[i]);
  263.                      }
  264.                    }
  265.                   fclose(fcfg);
  266.                   strcpy(args[j], "-cfg"); arglist[j] = args[j]; j++;
  267.                   strcpy(args[j], "temp.cfg"); arglist[j] = args[j]; j++;
  268.                  }
  269.                 else {
  270.                   /* Define strings on command line ***********************/
  271.                   for (i=0; i< 16; i++) { /* Add string definitions *******/
  272.                     if (strname[i][0] != '\0'  && strings[i][0] != '\0') {
  273.                       strcpy(args[j], "-def"); arglist[j] = args[j]; j++;
  274.                       strcpy(args[j], strname[i]); arglist[j] = args[j]; j++;
  275.                       strcpy(args[j], strings[i]); arglist[j] = args[j]; j++;
  276.                      }
  277.                    }
  278.                  }
  279.  
  280.                 i = fstnum; /* The argument string for first file name **/
  281.                 while (i <= lstnum) {
  282.                   strcpy(args[j], argv[i++]); arglist[j] = args[j]; j++;
  283.                  }
  284.  
  285.                 /*** Now the fax number *********************************/
  286.                 strcpy(args[j], faxnumber); arglist[j] = args[j]; j++;
  287.                 arglist[j] = NULL;
  288.                 /* Now all arguments of the QFAX command line are ready */
  289.                 _strtime(str2);      /* What's the starting time of fax */
  290.                 k = spawnv(_P_WAIT, qfaxcmd, arglist); total ++;
  291.                 if (k) failure ++; else successful ++;
  292.                 if ((fFaxLog = fopen(logName, "ab")) == NULL) {}
  293.                 else {
  294.                   fprintf(fFaxLog, "%s Faxto: %s \t", str2, faxnumber);
  295.                   fprintf(fFaxLog, "result: %d\r\n", k);
  296.                   fclose(fFaxLog);
  297.                  }
  298.                 cleanstr(); /* Clean up all string definition */
  299.                 break;
  300.        default: /* The line is ignored */ break;
  301.       }
  302.    }
  303.   fclose(fFaxList);
  304.   printf("\nAll faxes are sent! Results are logged in %s\n", logName);
  305.   printf("Total number of faxes sent: %d\n", total);
  306.   printf("Number of successful faxes: %d\n", successful);
  307.   printf("Number of faxes with error: %d\n", failure);
  308. }
  309.  
  310.